import { Body, Controller, Get, Post } from '@nestjs/common';
import { OpenRouterService } from '../services/openrouter.service';
import { OpenRouterCompletionRequestDto } from '../dto/openrouter/completion-request.dto';
import { OpenRouterChatCompletionRequestDto } from '../dto/openrouter/chat-completion-request.dto';
import {
  TextCompletionResponseDto,
  ChatCompletionResponseDto,
} from '../dto/completion-response.dto';
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';

@ApiTags('ai', 'openrouter')
@Controller('api/openrouter')
export class OpenRouterController {
  constructor(private readonly openRouterService: OpenRouterService) {}

  /**
   * 生成文本完成
   * @param request 完成请求
   * @returns 文本完成响应
   */
  @ApiOperation({
    summary: '生成文本完成',
    description: '使用OpenRouter API生成文本完成',
  })
  @ApiBody({ type: OpenRouterCompletionRequestDto })
  @ApiResponse({
    status: 200,
    description: '成功生成文本完成',
    type: TextCompletionResponseDto,
  })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 500, description: 'OpenRouter API调用失败' })
  @Post('completions')
  async generateCompletion(
    @Body() request: OpenRouterCompletionRequestDto,
  ): Promise<TextCompletionResponseDto> {
    return this.openRouterService.generateCompletion(request);
  }

  /**
   * 生成聊天完成
   * @param request 聊天完成请求
   * @returns 聊天完成响应
   */
  @ApiOperation({
    summary: '生成聊天完成',
    description: '使用OpenRouter API生成聊天完成，支持多模态内容（文本和图像）',
  })
  @ApiBody({ type: OpenRouterChatCompletionRequestDto })
  @ApiResponse({
    status: 200,
    description: '成功生成聊天完成',
    type: ChatCompletionResponseDto,
  })
  @ApiResponse({ status: 400, description: '请求参数错误' })
  @ApiResponse({ status: 500, description: 'OpenRouter API调用失败' })
  @Post('chat/completions')
  async generateChatCompletion(
    @Body() request: any,
  ): Promise<ChatCompletionResponseDto> {
    // 使用any类型跳过验证，直接将请求传递给服务
    // 这允许复杂类型如多模态内容传递，而不受Nest的DTO验证影响
    return this.openRouterService.generateChatCompletion(request);
  }

  /**
   * 获取OpenRouter可用模型列表
   * @returns 模型列表信息
   */
  @ApiOperation({
    summary: '获取可用模型列表',
    description: '获取OpenRouter可用的AI模型列表',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取模型列表',
    schema: {
      type: 'object',
      properties: {
        data: {
          type: 'array',
          items: {
            type: 'object',
            properties: {
              id: { type: 'string', example: 'anthropic/claude-3-sonnet' },
              name: { type: 'string', example: 'Claude 3 Sonnet' },
            },
          },
        },
      },
    },
  })
  @ApiResponse({ status: 500, description: 'OpenRouter API调用失败' })
  @Get('models')
  async getModels() {
    return this.openRouterService.getModels();
  }

  /**
   * 获取用户信用点数
   * @returns 用户信用点数信息
   */
  @ApiOperation({
    summary: '获取信用点数',
    description: '获取OpenRouter账户的剩余信用点数',
  })
  @ApiResponse({
    status: 200,
    description: '成功获取信用点数',
    schema: {
      type: 'object',
      properties: {
        credits: { type: 'number', example: 10.5 },
        credits_used: { type: 'number', example: 5.2 },
      },
    },
  })
  @ApiResponse({ status: 500, description: 'OpenRouter API调用失败' })
  @Get('credits')
  async getCredits() {
    return this.openRouterService.getCredits();
  }
}
